﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Web.Model;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Utility;
using Portal.Core;

namespace Portal.Cms.Render
{
    public class ChangePasswordRenderBll
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html =
                    "<script language=javascript> \r\n" +
                    "    function DoChangePassword() {\r\n" +
                    "        SiteId='" + SiteId + "';\r\n" +
                    "        LoginName='" + WebSessionUtility.GetCurrentLoginUser().LoginName + "';\r\n" +
                    "        Password= document.getElementById('txtOldPassword').value;\r\n" +
                    "        NewPassword = document.getElementById('txtNewPassword').value;\r\n" +
                    "        ReNewPassword = document.getElementById('txtReNewPassword').value;\r\n" +
                    "        AjaxOut = Portal.Cms.Render.ChangePasswordRenderBll.ServerSideChangePassword(SiteId, LoginName,Password, NewPassword, ReNewPassword).value;\r\n" +
                    "        if (AjaxOut.Error) {\r\n" +
                    "            alert(AjaxOut.InfoMessage);\r\n" +
                    "            return;\r\n" +
                    "        }\r\n" +
                    "        alert(AjaxOut.InfoMessage);\r\n" +
                    "        window.open(AjaxOut.RetUrl, '_self');\r\n" +
                    "    }\r\n" +

                    "   function BackHome()\r\n" +
                    "   {\r\n" +
                    "        window.open('Home.aspx', '_self');\r\n" +
                    "   }\r\n" +
                    "</script>\r\n" +

                   "<div class=formChangePassword>\r\n" +
                   "    <div class=title>ĐỔI MẬT KHẨU TRUY CẬP</div>\r\n" +
                   "    <div class=body>\r\n" +
                   "        <div style=\"margin-top:2px;text-align:left;padding:8px;font-size:22px;\">\r\n" +
                   "            <div style=\"font-size:16px;margin-top:4px;padding:4px;color:Gray;font-style:italic\">Chào mừng bạn đã đến với hệ thống quản lý website</div>\r\n" +
                   "        </div>\r\n" +
                   "        <div class=divInfo>\r\n" +
                    "                        <table>\r\n" +
                    "                            <tr>\r\n" +
                    "                                <td>Tài khoản:</td>\r\n" +
                    "                                <td>" + WebSessionUtility.GetCurrentLoginUser().LoginName + "</td>\r\n" +
                    "                            </tr>\r\n" +
                    "                            <tr>\r\n" +
                    "                                <td>Mật khẩu cũ:</td>\r\n" +
                    "                                <td><input id=txtOldPassword type=password class=textbox value=\"\"></td>\r\n" +
                    "                            </tr>\r\n" +
                    "                            <tr>\r\n" +
                    "                                <td>Mật khẩu mới:</td>\r\n" +
                    "                                <td><input id=txtNewPassword type=password class=textbox value=\"\"></td>\r\n" +
                    "                            </tr>\r\n" +
                    "                            <tr>\r\n" +
                    "                                <td>Gõ lại mật khẩu mới:</td>\r\n" +
                    "                                <td><input id=txtReNewPassword type=password class=textbox value=\"\"></td>\r\n" +
                    "                            </tr>\r\n" +
                    "                            <tr>\r\n" +
                    "                                <td></td>\r\n" +
                    "                                <td><input type=button class=button value=\"Đổi mật khẩu\" onclick=\"javascript:DoChangePassword();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:BackHome();\"></td>\r\n" +
                    "                            </tr>\r\n" +
                    "                            </tr>\r\n" +
                    "                        </table>\r\n" +


                   "</div>\r\n" +
                   "</td>\r\n" +
                   "</tr>\r\n" +
                   "</table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>" + ex.Message.ToString() + "</div>\r\n";
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideChangePassword(
            string SiteId, 
            string LoginName, 
            string OldPassword, 
            string NewPassword,
            string ReNewPassword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ChangePassword(SiteId, AssetCode, LoginName, OldPassword, NewPassword, ReNewPassword);
                OAjaxOut.InfoMessage = "Đổi mật khẩu truy cập cho tài khoản " + LoginName + " thành công!";
                OAjaxOut.RetUrl = "Home.aspx";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
